package com.mt.fpb.controller;

import cn.hutool.crypto.SecureUtil;
import com.mt.fpb.mapper.PayPasswordMapper;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Security;
import java.util.Random;

/**
 * FileName: PayPasswordController
 * Author:   wang
 * Date:     2021/8/14 17:42
 */

@RestController
@RequestMapping("password")
public class PayPasswordController {
    @Resource
    private PayPasswordMapper payPasswordMapper;

    @PostMapping("/update")
    public CommonResult updatePassword(String employeeId, String newPass, String oldPass) {
        String Soe = SecureUtil.md5().digestHex(oldPass);
        String Sne = SecureUtil.md5().digestHex(newPass);
        String oldPassword = payPasswordMapper.selectOldPassword(employeeId);
        if (Soe.equals(oldPassword)) {
            payPasswordMapper.updatePassword(employeeId, Sne);
        } else {
            return CommonResult.fail(500, "旧密码错误");
        }
        return CommonResult.success("修改成功", 200);
    }

    @PostMapping("/forget")
    public CommonResult forgetPassword(String employeeId, String newPass, String idCard, String name) {
        String Snp = SecureUtil.md5().digestHex(newPass);

        if (idCard.equals(payPasswordMapper.selectIdCard(employeeId)) && name.equals(payPasswordMapper.selectName(employeeId))) {
            payPasswordMapper.updatePassword(employeeId, Snp);
            return CommonResult.success("重置密码成功", 500);
        } else {
            return CommonResult.fail(200, "用户身份验证失败");
        }

    }
    @GetMapping("/cheek")
    public CommonResult cheekPassword(String employeeId,String password){
        String oldPassword = payPasswordMapper.selectOldPassword(employeeId);
        String pwd= SecureUtil.md5().digestHex(password);
        if (pwd.equals(oldPassword)){
            return CommonResult.success("密码验证成功",200);
        }
        return CommonResult.fail(500,"密码错误");
    }
}
